package com.iteaj.iot.test.client.fixed;

import com.iteaj.iot.Message;
import com.iteaj.iot.client.ClientProtocolHandle;
import com.iteaj.iot.client.IotClientBootstrap;
import com.iteaj.iot.config.ConnectProperties;
import com.iteaj.iot.consts.ExecStatus;
import com.iteaj.iot.test.IotTestProperties;
import com.iteaj.iot.test.TestConst;
import com.iteaj.iot.test.TestProtocolType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.time.Duration;
import java.time.Instant;

public class FixedLengthRequestHandle implements ClientProtocolHandle<FixedLengthRequestProtocol>, InitializingBean {

    @Autowired
    private ThreadPoolTaskScheduler scheduler;
    @Autowired
    private FixedLengthClientComponent component;
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public Object handle(FixedLengthRequestProtocol protocol) {
        Message.MessageHead head = protocol.requestMessage().getHead();
        String equipCode = head.getEquipCode();
        String desc = "固定长度解码组件测试";

        if(protocol.getExecStatus() == ExecStatus.success) {
            logger.info(TestConst.LOGGER_PROTOCOL_CON_DESC, component.getName()
                    , desc, protocol.getClientNum(), equipCode, head.getMessageId(), "通过");
        } else {
            logger.warn(TestConst.LOGGER_PROTOCOL_CON_DESC, component.getName()
                    , desc, protocol.getClientNum(), equipCode, head.getMessageId(), "不通过(" + protocol.getExecStatus().desc + ")");
        }
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        scheduler.schedule(() -> {
            ConnectProperties config = component.getConfig();
            if(config instanceof IotTestProperties.TestConnectConfig) {
                int clientNum = ((IotTestProperties.TestConnectConfig) config).getNum();
                if(clientNum > 0) {
                    for(int i=0; i< clientNum; i++) {
                        ConnectProperties properties = new ConnectProperties();
                        BeanUtils.copyProperties(config, properties);
                        FixedLengthClient client = new FixedLengthClient(component, properties);
                        client.init(IotClientBootstrap.clientGroup);
                        client.connect(null, 0);
                    }
                }
            }


        }, Instant.now().plusSeconds(20));
        scheduler.scheduleAtFixedRate(() -> {
            component.clients().forEach(item -> {
                FixedLengthClient client = (FixedLengthClient) item;
                FixedLengthClientMessage build = FixedLengthClientMessage.build(client.getDeviceSn(), TestProtocolType.CIReq);
                new FixedLengthRequestProtocol(build).request(client.getConfig());
            });


        }, Instant.now().plusSeconds(30), Duration.ofMillis(10 * 1000));
    }
}
